package com.jiyongjun.bms.base.module.user.controller;

import com.github.pagehelper.PageInfo;
import com.jiyongjun.bms.base.module.user.entity.UserEntity;
import com.jiyongjun.bms.base.module.user.entity.UserQueryModel;
import com.jiyongjun.bms.base.module.user.service.UserService;
import com.jiyongjun.bms.tool.ParamValidator;
import com.jiyongjun.bms.tool.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <h2>控制层</h2>
 * <p>
 *  描述：
 * </p>
 * 
 * @author 冀大侠
 * @since 1.0 2019-06-03 23:12:15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public PageInfo<UserEntity> getByCondition(UserQueryModel queryModel) {
        List<UserEntity> list = userService.getByCondition(queryModel);
        return new PageInfo<>(list);
    }

    @RequestMapping("/{id}")
    public UserEntity get(@PathVariable("id") Long id) {
        UserEntity entity = userService.getById(id);
        return entity;
    }

    @PostMapping
    public UserEntity add(UserEntity entity) {
        entity.setCreateTime(new Date());
        entity = userService.insert(entity);
        return entity;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        userService.delete(id);
        return Result.success(1);
    }

    @PutMapping
    public Result<Integer> update(UserEntity entity) {
        ParamValidator.checkRequired(entity.getId(), "id不能为空");
        Integer effectRows = userService.update(entity);
        entity.setModifiedTime(new Date());
        return Result.success(effectRows);
    }

    @PatchMapping
    public Result<Integer> patchUpdate(UserEntity entity) {
        ParamValidator.checkRequired(entity.getId(), "id不能为空");
        Integer effectRows = userService.patchUpdate(entity);
        entity.setModifiedTime(new Date());
        return Result.success(effectRows);
    }
}